home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 104_01 / c8.c < prev    next >
Text File  |  1980-01-01  |  7KB  |  402 lines

  1.  
  2. #ifndef    TRUE    /* check to see if include file is needed */
  3. #include <C.DEF>
  4. #endif
  5.  
  6. /* Begin a comment line for the assembler */
  7. comment()
  8. {
  9.     outbyte(';');
  10.     }
  11. /* Print all assembler info before any code is generated */
  12. header()
  13. {
  14.     outstr("@start:\tcsect");nl();
  15.     ol("jmp\t@init");
  16.     ol("extrn\t@and,@asl,@asr,@comp,@com,@div");
  17.     ol("extrn\t@gchar,@gint,@incdec");
  18.     ol("extrn\t@mult,@neg,@nlog,@or,@pint,@sub");
  19.     ol("extrn\t@sxt,@ucomp,@xor,@init,@switch");
  20.     ol("extrn\t@gintss,@gintsl,@gcharss,@gcharsl");
  21.     ol("extrn\t@pintss,@pintsl,@pcharss,@pcharsl");
  22.  
  23.     outstr("@eq\tequ\t@comp");nl();
  24.     outstr("@ge\tequ\t@comp+35h");nl();
  25.     outstr("@gt\tequ\t@comp+1ah");nl();
  26.     outstr("@le\tequ\t@comp+43h");nl();
  27.     outstr("@lt\tequ\t@comp+28h");nl();
  28.     outstr("@ne\tequ\t@comp+0dh");nl();
  29.     outstr("@uge\tequ\t@ucomp+6ch");nl();
  30.     outstr("@ugt\tequ\t@ucomp+51h");nl();
  31.     outstr("@ule\tequ\t@ucomp+79h");nl();
  32.     outstr("@ult\tequ\t@ucomp+5fh");nl();
  33.  
  34.     outstr("@preinc\tequ\t@incdec");nl();
  35.     outstr("@postinc\tequ\t@incdec+10h");nl();
  36.     outstr("@predec\tequ\t@incdec+08h");nl();
  37.     outstr("@postdec\tequ\t@incdec+1ah");nl();
  38.     }
  39. /* Print any assembler stuff needed after all code */
  40. trailer()
  41. {
  42.     ol("end");
  43.     }
  44. /*                            */
  45. /*    rewritten  4/30/81 by Mike Bernson        */
  46. /*                            */
  47. /* Fetch a static memory cell into the primary register */
  48. getmem(sname,typ,off)
  49. char *sname;
  50. int  typ;
  51. int  off;
  52. {
  53.     if(typ==cchar) {
  54.         ot("lda\t");
  55.         outstr(sname);
  56.         if (off) {
  57.             outstr("+");
  58.             outdec(off);
  59.             }
  60.         nl();
  61.         call("@sxt");
  62.         }
  63.     else {
  64.         ot("lhld\t");
  65.         outstr(sname);
  66.         if (off) {
  67.             outstr("+");
  68.             outdec(off);
  69.             }
  70.         nl();
  71.         }
  72.     }
  73. /*                            */
  74. /*    written by Mike Bernson 6/8/81            */
  75. /*                            */
  76. /*    load a byte offset from current stack        */
  77. /*                            */
  78. getmem_stack(lval,typ)
  79. int  *lval;
  80. char *typ;
  81. {
  82.     char *ptr;
  83.     int  off;
  84.  
  85.     ptr=lval[0];
  86.     off=ptr[offset]+ptr[offset+1]*256+lval[2]-sp;
  87.     
  88.     if (typ == cint)
  89.         if (isbyte(off)) call("@gintss"); else call("@gintsl");
  90.     else
  91.         if (isbyte(off)) call("@gcharss"); else call("@gcharsl");
  92.     defvalue(off);
  93.     }
  94. /*                            */
  95. /*    rewritten 4/30/81 By Mike Bernson        */
  96. /*                            */    
  97. /* Store the primary register into the specified    */
  98. /*    static memory                    */
  99. putmem(sname,typ,off)
  100. char *sname;
  101. int typ;
  102. int off;
  103. {
  104.     if (typ==cchar) {
  105.         ol("mov\ta,l");
  106.         ot("sta\t");
  107.         }
  108.     else ot("shld\t");
  109.     outstr(sname);
  110.     if (off) {
  111.         outstr("+");
  112.         outdec(off);
  113.         }
  114.     nl();
  115.     }
  116.  
  117. /* Store the specified object type in the primary register */
  118. /*      at the address on the top of the stack */
  119. putstk(typeobj)
  120. char typeobj;
  121. {
  122.     pop();
  123.     putsec(typeobj);
  124.     }
  125. /* store the specified object type in the primary register */
  126. /*    at the address in the secondary register    */
  127. putsec(typeobj)
  128. char typeobj;
  129. {
  130.     if (typeobj == cchar) {
  131.         ol("mov\ta,l");
  132.         ol("stax\td");
  133.         }
  134.     else call("@pint");
  135.     }
  136. /*                    */
  137. /*    written by Mike Bernson 6/9/81    */
  138. /*                    */
  139. /*    store hl value after call plus    */
  140. /*    current stack            */
  141. /*                    */
  142. putmem_stack(lval,typ)
  143. int  *lval;
  144. char typ;
  145. {
  146.     int off;
  147.     char *ptr;
  148.  
  149.     ptr=lval[0];
  150.     off=ptr[offset]+ptr[offset+1]*256+lval[2]-sp;
  151.  
  152.     if (typ == cint) 
  153.         if (isbyte(off)) call("@pintss"); else call("@pintsl");
  154.     else
  155.         if (isbyte(off)) call("@pcharss"); else call("@pcharsl");
  156.     defvalue(off);
  157.     }
  158.  
  159. /* Fetch the specified object type indirect through the */
  160. /*      primary register into the primary register */
  161. indirect(typeobj)
  162. char typeobj;
  163. {
  164.     if (typeobj == cchar) call("@gchar");
  165.     else call("@gint");
  166.     }
  167. /* add primary and secondary registers */
  168. add_address()
  169. {
  170.     ol("dad\td");
  171.     }
  172. /* Swap the primary and secondary registers */
  173. swap()
  174. {
  175.     ol("xchg");
  176.     }
  177. /* Print partial instruction to get an immediate value */
  178. /*      into the primary register */
  179. immed()
  180. {
  181.     ot("lxi\th,");
  182.     }
  183. /* Push the primary register onto the stack */
  184. push()
  185. {
  186.     ol("push\th");
  187.     sp=sp-2;
  188.     }
  189. /* push the secondary register    on stack */
  190. pushs()
  191. {
  192.     ol("push\td");
  193.     sp=sp-2;
  194.     }
  195. /* Pop the top of the stack into the secondary register */
  196. pop()
  197. {
  198.     ol("pop\td");
  199.     sp=sp+2;
  200.     }
  201. /* Swap the primary register and the top of the stack */
  202. swapstk()
  203. {
  204.     ol("xthl");
  205.     }
  206. /* call routine and subtract 2    from stack pointer */
  207. ccall(sname)
  208. char *sname;
  209. {
  210.     sp=sp+2;
  211.     call(sname);
  212.     }
  213. /* Call the specified subroutine name */
  214. call(sname)
  215. char *sname;
  216. {
  217.     ot("call\t");
  218.     outstr(sname);
  219.     nl();
  220.     }
  221. /* Return from subroutine */
  222. ret()
  223. {
  224.     ol("ret");
  225.     }
  226. /* Perform subroutine call to calue on top of stack */
  227. callstk()
  228. {
  229.     immed();
  230.     outstr("S+5");
  231.     nl();
  232.     swapstk();
  233.     ol("pchl");
  234.     sp=sp+2;
  235.     }
  236. /* Jump to specified internal label number */
  237. jump(label)
  238. int label;
  239. {
  240.     ot("jmp\t");
  241.     printlabel(label);
  242.     nl();
  243.     }
  244. /* test the primary register and jump if treu to label */
  245. truejump(label,status)
  246. int label;
  247. int status;
  248. {
  249.     if (!status) {
  250.         ol("mov\ta,h");
  251.         ol("ora\tl");
  252.         }
  253.     ot("jnz\t");
  254.     printlabel(label);
  255.     nl();
  256.     }
  257.  
  258.  
  259. /* Test the primary register and jump if false to label */
  260. testjump(label,status)
  261. int label;
  262. int status;
  263. {
  264.     if (!status) {
  265.         ol("mov\ta,h");
  266.         ol("ora\tl");
  267.         }
  268.     ot("jz\t");
  269.     printlabel(label);
  270.     nl();
  271.     }
  272. /* routine to search switch table */
  273. exec_switch(count,label,end_label)
  274. int count;    /* number of case statement in switch */
  275. int label;    /* label for switch table */
  276. int end_label;    /* label to execute when table search ends and not found */
  277. {
  278.     ot("lxi\td,");
  279.     printlabel(label);
  280.     nl();
  281.     ot("lxi\th,");
  282.     printlabel(end_label);
  283.     nl();
  284.     ot("mvi\tb,");
  285.     outdec(count);
  286.     nl();
  287.     ot("jmp\t@switch");
  288.     nl();
  289.     }
  290. /* output extrn and symbol name */
  291. extrn(n)
  292. char *n;
  293. {
  294.     outstr("\textrn\t");
  295.     outstr(n);nl();
  296.     }
  297.  
  298. /* output control section  name and key word */
  299. csect(n)
  300. char *n;
  301. {
  302.     outstr(n);
  303.     col();
  304.     ol("CSECT");
  305.     }
  306.  
  307. /* output data section name and key word */
  308. dsect(n)
  309. char *n;
  310. {
  311.     outstr(n);
  312.     col();
  313.     ol("dsect");
  314.     }
  315. /* define value using min amount of stroage space */
  316. defvalue(num)
  317. int num;
  318. {
  319.     if (isbyte(num)) defbyte(); else defword();
  320.     outdec(num);
  321.     nl();
  322.     }
  323.  
  324. /* check to see if number is a byte */
  325. isbyte(num)
  326. int num;
  327. {
  328.     return (num >= 0 && num <=255);
  329.     }
  330.  
  331. /* Print pseudo-op to define a byte */
  332. defbyte()
  333. {
  334.     ot("db\t");
  335.     }
  336. /* Print pseudo-op to define storage */
  337. defstorage()
  338. {
  339.     ot("ds\t");
  340.     }
  341. /* Print pseudo-op to define a word */
  342. defword()
  343. {
  344.     ot("dw\t");
  345.     }
  346. /* Modify the stack pointer to the new value indicated */
  347. modstk(newsp)
  348. int newsp;
  349. {
  350.     int k;
  351.     k=newsp-sp;
  352.     if (!k) return newsp;
  353.     if ( k >= 0) {
  354.         if(k<7) {
  355.             if (k&1) {
  356.                 ol("inx\tsp");
  357.                 k--;
  358.                 }
  359.             while(k) {
  360.                 ol("pop\tb");
  361.                 k=k-2;
  362.                 }
  363.             return newsp;
  364.             }
  365.         }
  366.     if (k<0) {
  367.         if (k>-7) {
  368.             if (k&1) {
  369.                 ol("dcx\tsp");
  370.                 k++;
  371.                 }
  372.             while(k) {
  373.                 ol("push\tb");
  374.                 k=k+2;
  375.                 }
  376.             return newsp;
  377.             }
  378.         }
  379.     swap();
  380.     immed();
  381.     outdec(k);
  382.     nl();
  383.     ol("dad\tsp");
  384.     ol("sphl");
  385.     swap();
  386.     return newsp;
  387.     }
  388. /*    Double the primary register    */
  389. doublereg()
  390. {
  391.     ol("dad\th");
  392.     }
  393.  
  394.  
  395. dad\th");
  396.     }
  397.  
  398.  
  399.     }
  400.  
  401.  
  402. CHOw#wt!GETCHï╡